/*
 * Created on Feb 1, 2005
 *
 * 
 */
package weather.ui.systray;

import java.io.InputStream;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

import weather.net.Temperature;

/**
 * @author Brian
 * 
 * 
 */
public class TrayGui {
    Display display;

    Shell shell;

    Shell s;

    Image image;

    final Tray tray;

    final TrayItem item;

    private static TrayGui _instance = null;

    public static TrayGui instance() {
        if (_instance == null) {
            _instance = new TrayGui();
            _instance.refresh();
        }
        return _instance;
    }

    private TrayGui() {
        display = new Display();
        shell = new Shell(display);
        s = new Shell(display);
        image = new Image(display, 16, 16);
        tray = display.getSystemTray();
        item = new TrayItem(tray, SWT.NONE);
        item.setToolTipText("Updating...");
        item.addListener(SWT.Show, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("show");
            }
        });
        item.addListener(SWT.Hide, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("hide");
            }
        });
        item.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("selection");
            }
        });
        item.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("default selection");
            }
        });
        final Menu menu = new Menu(shell, SWT.POP_UP);

        final MenuItem itemGraph = new MenuItem(menu, SWT.NULL);
        itemGraph.setText("Show Graph...");
        itemGraph.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
             GraphForm graphForm = new GraphForm(display);
             graphForm.show();
            }
        });

        new MenuItem(menu, SWT.SEPARATOR);

        final MenuItem itemRefresh = new MenuItem(menu, SWT.NULL);
        itemRefresh.setText("Refresh");
        itemRefresh.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                refresh();
            }
        });

        final MenuItem itemPreferences = new MenuItem(menu, SWT.NULL);
        itemPreferences.setText("Configure Proxy Server");
        itemPreferences.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                PreferenceForm pf = new PreferenceForm(display);
                pf.show();
            }
        });

        new MenuItem(menu, SWT.SEPARATOR);
        final MenuItem itemForecast = new MenuItem(menu, SWT.NULL);
        itemForecast.setText("Forecast");
        itemForecast.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                ForecastForm ff = new ForecastForm(display);
                ff.show();
            }
        });
        
        new MenuItem(menu, SWT.SEPARATOR);
        final MenuItem itemExit = new MenuItem(menu, SWT.NULL);
        itemExit.setText("Exit");
        itemExit.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event arg0) {
                shell.dispose();
            }
        });

        item.addListener(SWT.MenuDetect, new Listener() {
            public void handleEvent(Event event) {
                menu.setVisible(true);
            }
        });

        shell.setBounds(50, 50, 300, 200);
        // shell.open();

        runTimer();

    }

    private void runTimer() {
        display.timerExec(300000, new Runnable() {
            public void run() {
                refresh();
                runTimer();
            }
        });
    }

    public void refresh() {

        Temperature.instance().updateTemperature();

        item.setToolTipText("The current temperature in Boulder is " + Temperature.instance().getTemperature() + " degrees");

        InputStream is = this.getClass().getResourceAsStream("/icons/" + Temperature.instance().getTemperature() + ".gif");
        Image g = new Image(display, is);
        item.setVisible(false);
        item.setImage(g);
        item.setVisible(true);
        System.out.println("The weather has been refreshed!!!");
    }

    public void eventLoop() {
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        image.dispose();
        display.dispose();
    }

}
